package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.CpcBbsPostMapper;
import com.ruoyi.community.utils.MessageUtil;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CpcBbsCommentMapper;
import com.ruoyi.community.service.ICpcBbsCommentService;

/**
 * 党员论坛评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-23
 */
@Service
public class CpcBbsCommentServiceImpl implements ICpcBbsCommentService 
{
    @Autowired
    private CpcBbsCommentMapper cpcBbsCommentMapper;

    @Autowired
    private CpcBbsPostMapper cpcBbsPostMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    private String toUrl = "/pages/cpc/postDetail/postDetail?id=";

    /**
     * 查询党员论坛评论
     * 
     * @param commentId 党员论坛评论ID
     * @return 党员论坛评论
     */
    @Override
    public CpcBbsComment selectCpcBbsCommentById(Long commentId)
    {
        return cpcBbsCommentMapper.selectCpcBbsCommentById(commentId);
    }

    /**
     * 查询党员论坛评论列表
     * 
     * @param cpcBbsComment 党员论坛评论
     * @return 党员论坛评论
     */
    @Override
    public List<CpcBbsComment> selectCpcBbsCommentList(CpcBbsComment cpcBbsComment)
    {
        cpcBbsComment.setCommentId(Long.valueOf(SecurityUtils.getCommunityId()));
        return cpcBbsCommentMapper.selectCpcBbsCommentList(cpcBbsComment);
    }

    @Override
    public List<CpcBbsCommentPack> getSortedCommentPackList(CpcBbsComment cpcBbsComment)
    {
        cpcBbsComment.setCommentId(Long.valueOf(SecurityUtils.getCommunityId()));
        List<CpcBbsComment> rawCommentList = cpcBbsCommentMapper.selectCpcBbsCommentList(cpcBbsComment);
        List<CpcBbsCommentPack> list = new ArrayList<>();
        // selectUserOnlyById
        for (int i = 0; i < rawCommentList.size(); i++) {
            CpcBbsComment item = rawCommentList.get(i);
            CpcBbsCommentPack cpcBbsCommentPack = new CpcBbsCommentPack();
            cpcBbsCommentPack.initFromCpcComment(item);
            SysUser sysUser = sysUserMapper.selectUserOnlyById(item.getUserId().toString());
            cpcBbsCommentPack.setUserName(sysUser.getUserName());
            if(sysUser.getAvatar()==null){
                cpcBbsCommentPack.setUserAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
            }else{
                cpcBbsCommentPack.setUserAvatar(sysUser.getAvatar());
            }
            if (item.getParentCommentId()!=null){
                CpcBbsComment commentGetReply = cpcBbsCommentMapper.selectCpcBbsCommentById(item.getParentCommentId());
                SysUser userGetReply = sysUserMapper.selectUserOnlyById(commentGetReply.getUserId().toString());
                cpcBbsCommentPack.setUserNameGetReply(userGetReply.getUserName());
            }else {
                cpcBbsCommentPack.setUserNameGetReply(null);
            }
            list.add(cpcBbsCommentPack);
        }
        return list;
    }

//    @Override
//    public List<CpcBbsComment> selectCpcBbsCommentByPostId(Long postId)
//    {
//        CpcBbsComment cpcBbsComment = new CpcBbsComment();
//        cpcBbsComment.setPostId(postId);
//        cpcBbsComment.setCommunityId(Long.valueOf(SecurityUtils.getCommunityId()));
//        return cpcBbsCommentMapper.selectCpcBbsCommentList(cpcBbsComment);
//    }

    /**
     * 新增党员论坛评论
     * 
     * @param cpcBbsComment 党员论坛评论
     * @return 结果
     */
    @Override
    public int insertCpcBbsComment(CpcBbsComment cpcBbsComment)
    {
        cpcBbsComment.setUserId(SecurityUtils.getUserID());
        cpcBbsComment.setCommunityId(Long.valueOf(SecurityUtils.getCommunityId()));
        int res = cpcBbsCommentMapper.insertCpcBbsComment(cpcBbsComment);

        if(res==1){
            // 更新帖子评论量与更新时间
            CpcBbsPost cpcBbsPost = cpcBbsPostMapper.selectCpcBbsPostById(cpcBbsComment.getPostId().toString());
            int commentNumber = cpcBbsPost.getCommentNumber() + 1;
            cpcBbsPost.setCommentNumber(commentNumber);
            cpcBbsPost.setPostUpdateTime(cpcBbsComment.getSyncTime());
            cpcBbsPostMapper.updateCpcBbsPost(cpcBbsPost);
            // 发送站内信给被评论用户或帖子
            if(!cpcBbsPost.getAuthorId().equals(SecurityUtils.getUserID())){
                Remind remind = new Remind();
                remind.setSenderId(SecurityUtils.getUserID());
                String username = SecurityUtils.getLoginUser().getUsername();
                remind.setSenderName(username);
                remind.setSenderAction(RemindAction.PINGLUN);
                remind.setUrl(toUrl+cpcBbsComment.getPostId());
                if(cpcBbsComment.getParentCommentId()==null){
                    // 给帖子的发布者发送站内信
                    Long recipientId = cpcBbsPostMapper.selectCpcBbsPostById(cpcBbsComment.getPostId().toString()).getAuthorId();
                    remind.setRecipientId(recipientId);
                    remind.setContent(username + "在您的文章中评论:");
                }else{
                    // 给被评论的用户发送站内信
                    Long recipientId = cpcBbsCommentMapper.selectCpcBbsCommentById(cpcBbsComment.getParentCommentId()).getUserId();
                    remind.setRecipientId(recipientId);
                    remind.setContent(username + "回复了您的评论:");
                }
                MessageUtil.addRemind(remind);
            }

        }
        return res;
    }

    /**
     * 修改党员论坛评论
     * 
     * @param cpcBbsComment 党员论坛评论
     * @return 结果
     */
    @Override
    public int updateCpcBbsComment(CpcBbsComment cpcBbsComment)
    {
        return cpcBbsCommentMapper.updateCpcBbsComment(cpcBbsComment);
    }

    /**
     * 批量删除党员论坛评论
     * 
     * @param commentIds 需要删除的党员论坛评论ID
     * @return 结果
     */
    @Override
    public int deleteCpcBbsCommentByIds(Long[] commentIds)
    {
        return cpcBbsCommentMapper.deleteCpcBbsCommentByIds(commentIds);
    }

    /**
     * 删除党员论坛评论信息
     * 
     * @param commentId 党员论坛评论ID
     * @return 结果
     */
    @Override
    public int deleteCpcBbsCommentById(Long commentId)
    {
        return cpcBbsCommentMapper.deleteCpcBbsCommentById(commentId);
    }
}
